package com.easyfun.healthmagicbox.inputviews;



import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.base.PicturePickerBaseActivity;
import com.googlecode.tesseract.android.TessBaseAPI;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class PictureTessActivity extends PicturePickerBaseActivity {
	
	//private static final int PHOTO_CAPTURE = 0x11;// ����
	//private static final int PHOTO_RESULT = 0x12;// ���

	private static String LANGUAGE = "eng";
	
	private static String TMP_FILENAME = "tmp.png";
	private static TextView tvResult;
	private static ImageView ivSelected;
	private static ImageView ivTreated;
	private static Button btnCamera;
	private static Button btnSelect;
	private static CheckBox chPreTreat;
	private static RadioGroup radioGroup;
	private static String textResult;
	private static Bitmap bitmapSelected;
	private static Bitmap bitmapTreated;
	private static final int SHOWRESULT = 0x101;
	private static final int SHOWTREATEDIMG = 0x102;

	// ��handler���ڴ����޸Ľ�������
	public static Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOWRESULT:
				if (textResult.equals(""))
					tvResult.setText("no result");
				else
					tvResult.setText(textResult);
				break;
			case SHOWTREATEDIMG:
				tvResult.setText("......");
				if (bitmapTreated!=null)
					showPicture(ivTreated, bitmapTreated);
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inputpicture);
/*
		tvResult = (TextView) findViewById(R.id.tv_result);
		ivSelected = (ImageView) findViewById(R.id.iv_selected);
		ivTreated = (ImageView) findViewById(R.id.iv_treated);
		btnCamera = (Button) findViewById(R.id.btn_camera);
		btnSelect = (Button) findViewById(R.id.btn_select);
		chPreTreat = (CheckBox) findViewById(R.id.ch_pretreat);
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);

		btnCamera.setOnClickListener(new cameraButtonListener());
		btnSelect.setOnClickListener(new selectButtonListener());

		// 
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_en:
					LANGUAGE = "eng";
					break;
				case R.id.rb_ch:
					LANGUAGE = "chi_sim";
					break;
				}
			}

		});
*/
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);

		Uri pictureUri = null;
		
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
				tvResult.setText("get photo from camera");
				pictureUri = TAKE_PHOTE_TEMP_URI;
				break;
			case CHOOSE_PICTURE:
				pictureUri = data.getData(); 
				break;
			default:
				tvResult.setText("error");
			}
			
			if (chPreTreat.isChecked())
				tvResult.setText("预处理");
			else
				tvResult.setText("不进行预处理");

			// zoom in scale is 6
            bitmapSelected = decodeUriAsBitmap(pictureUri,6);
			
			
			// 
			showPicture(ivSelected, bitmapSelected);
			
			// put into a new thread
			new Thread(new Runnable() {
				@Override
				public void run() {
					if (chPreTreat.isChecked()) {
						//bitmapTreated = ImgPretreatment
						//		.doPretreatment(bitmapSelected);
						Message msg = new Message();
						msg.what = SHOWTREATEDIMG;
						myHandler.sendMessage(msg);
						textResult = doOcr(bitmapSelected, LANGUAGE);
					} else {
						//bitmapTreated = ImgPretreatment
						//		.converyToGrayImg(bitmapSelected);
						Message msg = new Message();
						msg.what = SHOWTREATEDIMG;
						myHandler.sendMessage(msg);
						textResult = doOcr(bitmapSelected, LANGUAGE);
					}
					Message msg2 = new Message();
					msg2.what = SHOWRESULT;
					myHandler.sendMessage(msg2);
				}

			}).start();

		}

	}
	
	// ����ʶ��
	class cameraButtonListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			showPicturePicker(PictureTessActivity.this);
		}
	};

	// �����ѡȡ��Ƭ���ü�
	class selectButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			showPicturePicker(PictureTessActivity.this);
		}

	}
	
	// ��ͼƬ��ʾ��view��
	public static void showPicture(ImageView iv, Bitmap bmp){
		iv.setImageBitmap(bmp);
	}
	
	/**
	 * ����ͼƬʶ��
	 * 
	 * @param bitmap
	 *            ��ʶ��ͼƬ
	 * @param language
	 *            ʶ������
	 * @return ʶ�����ַ�
	 */
 	public String doOcr(Bitmap bitmap, String language) {
 		//bitmap = ImageTools.zoomBitmap(bitmap, 800, 600);
		TessBaseAPI baseApi = new TessBaseAPI();
		String trainpath = getStorePath();
		baseApi.init(trainpath, language);

		// ����Ӵ��У�tess-twoҪ��BMP����Ϊ������
		Bitmap bitmap8888 = bitmap.copy(Bitmap.Config.ARGB_8888, true);

		baseApi.setImage(bitmap8888);

		String text = baseApi.getUTF8Text();

		baseApi.clear();
		baseApi.end();

		return text;
	}

	@Override
	protected String getImageFileName(String m_personID2) {
		// TODO Auto-generated method stub
		return null;
	}


	

    
}
